import axios from 'axios';
import { Loading } from 'element-ui';
let base = 'http://www.bestckd.com/lungtest/api'

export const login = params => { return axios.post(`${base}/admin/login`, params).then(res => res.data); };
export const action = {
    patient:{
        add:`${base}/patient/createPatient`,
    }
};
let loadingobject="";
// ----------------------------------添加请求拦截器
axios.interceptors.request.use(function (config) {
    // 在发送请求之前做些什么
    //---------------请求
    return config;
}, function (error) {
// 对请求错误做些什么
    //-----------请求失败
    return Promise.reject(error);
});
var _that = this;
// ----------------------------------------------添加响应拦截器
axios.interceptors.response.use(function (response) {
    // 对响应数据做点什么
    if (!response.data.IsSuccess) {
        if(window.vue && window.vue.$message){
            window.vue.$message({
                // message: response.data.ErrorMessage,
                message: "网络异常请稍等",
                type: "error"
            });
        }
    }
    else if(response.data.IsSuccess){
        setTimeout(()=>{
            var removePage=document.getElementsByClassName('el-loading-mask');
            if(removePage){
                if(removePage.length>0){
                    for(var i=0;i<removePage.length;i++){
                        removePage[i].remove();      
                    }  
                }  
            }
        },100);
        
    }
    //---------------请求成功
    return response;
},  //-----------请求失败
function (error) {
    if(window.vue && window.vue.$message){
        window.vue.$message({
            message: "网络异常请稍等",
            type: "error"
        });
    }
    // 对响应错误做点什么
    return Promise.reject(error);
});


const post = function(url,params,option){
    var token = "";
    let user = JSON.parse(sessionStorage.getItem('user'));
    // console.log(user);
    if (user.Token) {
        token = user.Token;
    }
if(option){
        if(option.ifLoading==true){
            if(option.loadingContent){
                Loading.service({target:option.loadingContent});
            }else{
                Loading.service();
            }
        }
    }else{
    }
    return axios({
        method: 'post',headers: {'Token': token},url: base+url,data: params}).then();

};

const get = function(url,params,option){
    var token = "";
    let user = JSON.parse(sessionStorage.getItem('user'));
    if (user.Token) {
        token = user.Token;
    }
 if(option){
        if(option.ifLoading==true){
            if(option.loadingContent){
                Loading.service({target:option.loadingContent});
            }
        }
    }
    var index = 0;
    var longUrl = base+url;
    for(var item in params){
        if(index==0){
            longUrl = longUrl + "?"+item+"="+params[item]
        }
        if(index!=0){
            longUrl = longUrl + "&"+item+"="+params[item]
        }
        index++;
    }

    return axios({
        method: 'get',headers: {'Token': token},url: longUrl}).then();
};



// export const storeConfirmOrder=params=>{return get('/order/storeConfirmOrder/'+params)}


//文章管理
export const createArticle=(params,option)=>{return post('/admin/createArticle',params,option);}
export const updateArticle=(params,option)=>{return post('/admin/updateArticle',params,option);}
export const getArticleList=(params,option)=>{return post('/admin/getArticleList',params,option);}
export const getDoctorList=(params,option)=>{return post('/user/getUserList',params,option);}
export const approvalDoctor=(param,option)=>{return post('/admin/approvalDoctor',param,option)} 
export const deleteArticle=(param,option)=>{return post('/admin/deleteArticle',param,option)}